package com.ruoyi.project.hcy.task.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hcy.administrations.domain.Crew;
import com.ruoyi.project.hcy.task.domain.Task;
import com.ruoyi.project.hcy.task.domain.TaskPostpone;
import com.ruoyi.project.hcy.task.service.ITaskPostponeService;
import com.ruoyi.project.hcy.task.service.ITaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务延期操作处理
 */
@Controller
@RequestMapping("/hcy/task/postpone")
public class TaskPostponeController extends BaseController {

    @Autowired
    ITaskPostponeService taskPostponeService;

    @Autowired
    ITaskService taskService;

    private String prefix = "hcy/task/taskpostpone";

    @RequiresPermissions("hcy:postpone:view")
    @GetMapping()
    public String postpone() {

        return prefix + "/postpone";

    }

    @RequiresPermissions("hcy:postpone:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TaskPostpone taskPostpone) {
        startPage();
        List<TaskPostpone> list = taskPostponeService.selectTaskPostponeList(taskPostpone);
        return getDataTable(list);
    }

    @RequiresPermissions("hcy:postpone:add")
    @GetMapping("/add")
    public String add() {

        return prefix + "/add";

    }

    @RequiresPermissions("hcy:postpone:verifyview")
    @GetMapping("/verifyview")
    public String verify() {

        return prefix + "/verifyview";

    }

    /**
     * 新增保存任务延期申请
     */
    @RequiresPermissions("hcy:postpone:add")
    @Log(title = "任务延期管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TaskPostpone taskPostpone, Long taskId, String taskEndTimeStr, Long userId) {

        return toAjax(taskPostponeService.insertPostpone(taskPostpone, taskId, taskEndTimeStr, userId));
    }

    @RequiresPermissions("hcy:postpone:add")
    @PostMapping("/crewOfTask")
    @ResponseBody
    public Crew crewOfTask(Long taskId) {
        return taskPostponeService.selectCrewOfTask(taskId);
    }

    @RequiresPermissions("hcy:postpone:add")
    @PostMapping("/taskList")
    @ResponseBody
    public List<Task> taskList() {
        return taskPostponeService.selectTaskList();
    }

    @RequiresPermissions("hcy:postpone:remove")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(taskPostponeService.deleteTaskPostponeByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改延期申请
     */
    @GetMapping("/edit/{postponeId}")
    public String edit(@PathVariable("postponeId") Long postponeId, ModelMap mmap) {
        mmap.put("taskPostpone", taskPostponeService.selectTaskPostponeById(postponeId));
        return prefix + "/edit";
    }

    /**
     * 修改保存延期申请
     */
    @RequiresPermissions("hcy:postpone:edit")
    @Log(title = "延期申请管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TaskPostpone taskPostpone,String taskEndTimeStr) {

        return toAjax(taskPostponeService.updateTaskPostpone(taskPostpone, taskEndTimeStr, null));
    }

    /**
     * 延期批复
     */
    @GetMapping("/verify/{postponeId}")
    public String verify(@PathVariable("postponeId") Long postponeId, ModelMap mmap) {
        TaskPostpone taskPostpone = taskPostponeService.selectTaskPostponeById(postponeId);
        mmap.put("taskPostpone", taskPostpone);
        mmap.put("task", taskPostpone.getTask());
        return prefix + "/verify";
    }

    /**
     * 修改保存延期申请
     */
    @RequiresPermissions("hcy:postpone:verify")
    @Log(title = "延期申请批复", businessType = BusinessType.UPDATE)
    @PostMapping("/verify")
    @ResponseBody
    public AjaxResult verifySave(@Validated TaskPostpone taskPostpone, String taskEndTimeStr, Long taskId) {

        return toAjax(taskPostponeService.updateTaskPostpone(taskPostpone, taskEndTimeStr, taskId));
    }


}
